/***************************************************************************
 *   This file is a part of match.                               *
 *   Copyright (C) 2009 by fu4ny 										   *
 *   ntcong.it@gmail.com                                                   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *  You should have received a copy of the GNU General Public License      * 
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>   *             *
 ***************************************************************************/

#include "ImageDef.h"
#include <fstream>
#include <iostream>
using namespace std;

ImageDef::ImageDef() {


}

ImageDef::~ImageDef() {

}

bool ImageDef::isExist( double id, double pose ) {
	key temp;
	temp.id = id;
	temp.pose = pose;
	return imgDef.find(temp) != imgDef.end();
}

void ImageDef::add( double id, double pose, double vector, const char* dest ) {
	if ( isExist(id,pose) ) return;
	key tempKey;
	tempKey.id = id;
	tempKey.pose = pose;
	Image temp;
	temp.id = id;
	temp.pose = pose;
	temp.vector = vector;
	temp.dest.assign(dest);
	imgDef.insert( pair<key,Image>(tempKey,temp) );
}

void ImageDef::load( const char* filename ) {
	ifstream data;
	data.open(filename);
	while (data.good()) {
	  	key tempKey;
	   	data >> tempKey.id >> tempKey.pose;
	   	if ( !isExist(tempKey.id, tempKey.pose) ) {
	   		Image temp;
	   		temp.id = tempKey.id;
	   		temp.pose = tempKey.pose;
	   		data >> temp.vector;
	   		char newDest[100];
	   		data.getline(newDest,100);
	   		temp.dest.assign(newDest);
	   		temp.dest.erase(0,1);
	   		imgDef.insert( pair<key,Image>(tempKey,temp) );
	   	}
    }
    data.close();
}

void ImageDef::write( const char* filename ) {
	ofstream ofs(filename);
	if ( !ofs ) {
		cerr << "Cannot open " << filename << " for writing!\n";
		return;
	}
	for ( tImgDef::const_iterator iter=imgDef.begin(); iter != imgDef.end(); iter++ ) {
		Image temp = iter->second;
		ofs << temp.id << " " << temp.pose << " " << temp.vector << " " << temp.dest << "\n";
	}
	ofs.close();
}

void ImageDef::remove( double id, double pose ) {
	key temp;
	temp.id = id;
	temp.pose = pose;
	imgDef.erase( temp );
}

bool operator<(const key &arg1, const key &arg2) {
		if ( arg1.id < arg2.id ) return true;
		else if ( arg1.id==arg2.id && arg1.pose < arg2.pose) return true;
		else return false;
}
